//
//  OrderSectionFooterView.m
//  WuJieLive
//
//  Created by iMac on 2017/12/21.
//  Copyright © 2017年 zhk. All rights reserved.
//
#define btnSize CGSizeMake(kScreenWidth / 5, 30)
#import "OrderSectionFooterView.h"

@implementation OrderSectionFooterView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setupView];
    }
    
    return self;
}

- (void)setupView
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.detailLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.top.offset(10);
    }];
    
    _commentBtn = [[UIButton alloc]init]; // 红
    [self.contentView addSubview:_commentBtn];
    _commentBtn.layer.borderColor = MainColor.CGColor;
    _commentBtn.layer.borderWidth = 0.5;
    _commentBtn.layer.masksToBounds = YES;
    _commentBtn.layer.cornerRadius = 10;
    [_commentBtn setTitleColor:MainColor forState:(UIControlStateNormal)];
    _commentBtn.titleLabel.font = FontSize(13);
    [_commentBtn setTitle:@"评价晒单" forState:(UIControlStateNormal)];
    [_commentBtn addTarget:self action:@selector(commentAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _lastCheckBtn = [[UIButton alloc]init];
    [self.contentView addSubview:_lastCheckBtn];
    _lastCheckBtn.layer.borderColor = TextAssistColor.CGColor;
    _lastCheckBtn.layer.borderWidth = 0.5;
    _lastCheckBtn.layer.masksToBounds = YES;
    _lastCheckBtn.layer.cornerRadius = 10;
    [_lastCheckBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _lastCheckBtn.titleLabel.font = FontSize(13);
    [_lastCheckBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    [_lastCheckBtn addTarget:self action:@selector(checkAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _deleteBtn = [[UIButton alloc]init];
    [self.contentView addSubview:_deleteBtn];
    _deleteBtn.layer.borderColor = TextAssistColor.CGColor;
    _deleteBtn.layer.borderWidth = 0.5;
    _deleteBtn.layer.masksToBounds = YES;
    _deleteBtn.layer.cornerRadius = 10;
    [_deleteBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _deleteBtn.titleLabel.font = FontSize(13);
    [_deleteBtn setTitle:@"删除订单" forState:(UIControlStateNormal)];
    [_deleteBtn addTarget:self action:@selector(deleAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _confirmGoodsBtn = [[UIButton alloc]init]; // 红
    [self.contentView addSubview:_confirmGoodsBtn];
    _confirmGoodsBtn.layer.borderColor = MainColor.CGColor;
    _confirmGoodsBtn.layer.borderWidth = 0.5;
    _confirmGoodsBtn.layer.masksToBounds = YES;
    _confirmGoodsBtn.layer.cornerRadius = 10;
    [_confirmGoodsBtn setTitleColor:MainColor forState:(UIControlStateNormal)];
    _confirmGoodsBtn.titleLabel.font = FontSize(13);
    [_confirmGoodsBtn setTitle:@"确认收货" forState:(UIControlStateNormal)];
    [_confirmGoodsBtn addTarget:self action:@selector(confirmGoodsAction) forControlEvents:(UIControlEventTouchUpInside)];

    
    _checkBtn = [[UIButton alloc]init];
    [self.contentView addSubview:_checkBtn];
    _checkBtn.layer.borderColor = TextAssistColor.CGColor;
    _checkBtn.layer.borderWidth = 0.5;
    _checkBtn.layer.masksToBounds = YES;
    _checkBtn.layer.cornerRadius = 10;
    [_checkBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _checkBtn.titleLabel.font = FontSize(13);
    [_checkBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    [_checkBtn addTarget:self action:@selector(checkAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    _rebackMoneyBtn = [[UIButton alloc]init];
    [self.contentView addSubview:_rebackMoneyBtn];
    _rebackMoneyBtn.layer.borderColor = TextAssistColor.CGColor;
    _rebackMoneyBtn.layer.borderWidth = 0.5;
    _rebackMoneyBtn.layer.masksToBounds = YES;
    _rebackMoneyBtn.layer.cornerRadius = 10;
    [_rebackMoneyBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _rebackMoneyBtn.titleLabel.font = FontSize(13);
    [_rebackMoneyBtn setTitle:@"申请退款" forState:(UIControlStateNormal)];
    [_rebackMoneyBtn addTarget:self action:@selector(rebackAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _payForBtn = [[UIButton alloc]init]; // 红
    [self.contentView addSubview:_payForBtn];
    _payForBtn.layer.borderColor = MainColor.CGColor;
    _payForBtn.layer.borderWidth = 0.5;
    _payForBtn.layer.masksToBounds = YES;
    _payForBtn.layer.cornerRadius = 10;
    [_payForBtn setTitleColor:MainColor forState:(UIControlStateNormal)];
    _payForBtn.titleLabel.font = FontSize(13);
    [_payForBtn setTitle:@"立即付款" forState:(UIControlStateNormal)];
    [_payForBtn addTarget:self action:@selector(payForAction) forControlEvents:(UIControlEventTouchUpInside)];

    
    _cancelOrderBtn = [[UIButton alloc]init];
    [self.contentView addSubview:_cancelOrderBtn];
    _cancelOrderBtn.layer.borderColor = TextAssistColor.CGColor;
    _cancelOrderBtn.layer.borderWidth = 0.5;
    _cancelOrderBtn.layer.masksToBounds = YES;
    _cancelOrderBtn.layer.cornerRadius = 10;
    [_cancelOrderBtn setTitleColor:TextAssistColor forState:(UIControlStateNormal)];
    _cancelOrderBtn.titleLabel.font = FontSize(13);
    [_cancelOrderBtn setTitle:@"取消订单" forState:(UIControlStateNormal)];
    [_cancelOrderBtn addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void)bindDataWithModel:(MyOrderDetailModel *)model
{
    // 订单状态 1待付款 6待发货 11待收货 16待评论 21关闭 30完成
    if ([model.status_order integerValue] == 16) {  // 带评论
        self.commentBtn.hidden = NO;
        self.lastCheckBtn.hidden = NO;
        self.deleteBtn.hidden = NO;  // 待评价的按钮
        
        self.checkBtn.hidden = YES; // 待收货的按钮
        self.confirmGoodsBtn.hidden = YES;
        
        self.rebackMoneyBtn.hidden = YES; // 待发货的按钮
        
        self.payForBtn.hidden = YES;   // 代付款的按钮
        self.cancelOrderBtn.hidden = YES;
        
        [self commont];
        
    }else if ([model.status_order integerValue] == 11){ // 待收货
        
        self.confirmGoodsBtn.hidden = NO;
        self.checkBtn.hidden = NO; // 待收货的按钮
        
        self.deleteBtn.hidden = YES;  // 待评价的按钮
        self.commentBtn.hidden = YES;
        self.lastCheckBtn.hidden = YES;
        
        self.rebackMoneyBtn.hidden = YES; // 待发货的按钮
        
        self.payForBtn.hidden = YES;   // 代付款的按钮
        self.cancelOrderBtn.hidden = YES;
        
        [self getGoods];
        
    }else if ([model.status_order integerValue] == 6){  // 待发货
        
        self.rebackMoneyBtn.hidden = NO; // 待发货的按钮
        
        self.payForBtn.hidden = YES;   // 代付款的按钮
        self.cancelOrderBtn.hidden = YES;
        
        self.checkBtn.hidden = YES; // 待收货的按钮
        self.confirmGoodsBtn.hidden = YES;
        
        self.deleteBtn.hidden = YES;  // 待评价的按钮
        self.commentBtn.hidden = YES;
        self.lastCheckBtn.hidden = YES;
        
        [self sendGoods];
        
    }else if ([model.status_order integerValue] == 1){ // 代付款
        
        self.payForBtn.hidden = NO;   // 代付款的按钮
        self.cancelOrderBtn.hidden = NO;
        
        self.checkBtn.hidden = YES; // 待收货的按钮
        self.confirmGoodsBtn.hidden = YES;
        
        self.rebackMoneyBtn.hidden = YES; // 待发货的按钮
        
        self.deleteBtn.hidden = YES;  // 待评价的按钮
        self.commentBtn.hidden = YES;
        self.lastCheckBtn.hidden = YES;
        
        [self payFor];
        
    }else if ([model.status_order integerValue] == 21){ // 关闭
        
        self.commentBtn.hidden = YES;
        self.lastCheckBtn.hidden = YES;
        self.deleteBtn.hidden = NO;  // 待评价的按钮
        
        self.checkBtn.hidden = YES; // 待收货的按钮
        self.confirmGoodsBtn.hidden = YES;
        
        self.rebackMoneyBtn.hidden = YES; // 待发货的按钮
        
        self.payForBtn.hidden = YES;   // 代付款的按钮
        self.cancelOrderBtn.hidden = YES;
        [self close];
        
    }else if ([model.status_order integerValue] == 30){ // 完成
        
        self.commentBtn.hidden = YES;
        self.lastCheckBtn.hidden = YES;
        self.deleteBtn.hidden = NO;  // 待评价的按钮
        
        self.checkBtn.hidden = NO; // 待收货的按钮
        self.confirmGoodsBtn.hidden = YES;
        
        self.rebackMoneyBtn.hidden = YES; // 待发货的按钮
        
        self.payForBtn.hidden = YES;   // 代付款的按钮
        self.cancelOrderBtn.hidden = YES;
        
        [self finish];
        
    }
    NSString * priceString = [NSString stringWithFormat:@"¥%@",model.price_total];
    NSString * string  = [NSString stringWithFormat:@"共%@件商品 合计:%@(含运费¥%@)",model.goods_total_number,priceString,model.price_fare];
    NSMutableAttributedString * attString = [[NSMutableAttributedString alloc]initWithString:string attributes:@{NSForegroundColorAttributeName:TextAssistColor,NSFontAttributeName:FontSize(13)}];
    [attString setAttributes:@{NSForegroundColorAttributeName:MainColor,NSFontAttributeName:FontSize(16)} range:[string rangeOfString:priceString]];
    self.detailLabel.attributedText = attString;
}

- (void)commont
{
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];

    [self.lastCheckBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.commentBtn.mas_bottom);
        make.right.equalTo(self.commentBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.commentBtn.mas_bottom);
        make.right.equalTo(self.lastCheckBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
}

- (void)getGoods
{
    [self.confirmGoodsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.confirmGoodsBtn.mas_bottom);
        make.right.equalTo(self.confirmGoodsBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
}

- (void)sendGoods
{
    [self.rebackMoneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
}

- (void)payFor
{
    [self.payForBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.cancelOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.payForBtn.mas_bottom);
        make.right.equalTo(self.payForBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
}

- (void)close
{
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
}

- (void)finish
{
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.right.offset(-15);
        make.size.mas_offset(btnSize);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.checkBtn.mas_bottom);
        make.right.equalTo(self.checkBtn.mas_left).offset(-15);
        make.size.mas_offset(btnSize);
    }];
}

#pragma mark 取消订单
- (void)cancelAction
{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark 立即付款
- (void)payForAction
{
    if (self.payForBlock) {
        self.payForBlock();
    }
}

#pragma mark 申请退款
- (void)rebackAction
{
    if (self.rebackBlock) {
        self.rebackBlock();
    }
}

#pragma mark 确认收货
- (void)confirmGoodsAction
{
    if (self.confirGoodsBlock) {
        self.confirGoodsBlock();
    }
}

#pragma mark 查看物流
- (void)checkAction
{
    if (self.checkBlock) {
        self.checkBlock();
    }
}

#pragma mark 立即评论
- (void)commentAction
{
    if (self.commentBlock) {
        self.commentBlock();
    }
}

#pragma mark 删除订单
- (void)deleAction
{
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}

@end
